<?php
/**
 * Created by PhpStorm.
 * User: liuxin
 * Date: 2017/12/31
 * Time: 下午1:34
 */
namespace custom\components;

use backend\models\rbac\AuthAssignment;
use yii;
use yii\base\Component;
use backend\components\AdminLog;
use yii\base\Event;
use yii\db\BaseActiveRecord;
use yii\web\Response;

class Custom extends Component
{

    public function __set($name, $value)
    {
        $this->$name = $value;
    }

    public function __get($name)
    {
        return $this->$name;
    }

    public static function backendInit()
    {
        //如果是管理员，记录操作日志
        // if(isset(Yii::$app->user->identity->id) && array_intersect(isset(Yii::$app->params['roles']['SaleAccessRoles']) ?: [],  AuthAssignment::getRoles())){
        if(!Yii::$app->user->isGuest){
            Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_INSERT, [
                AdminLog::className(),
                'create'
            ]);
            Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_UPDATE, [
                AdminLog::className(),
                'update'
            ]);
            Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_DELETE, [
                AdminLog::className(),
                'delete'
            ]);
            Event::on(BaseActiveRecord::className(), BaseActiveRecord::EVENT_AFTER_FIND, function ($event) {
                if (isset($event->sender->updated_at) && $event->sender->updated_at == 0) {
                    $event->sender->updated_at = null;
                }
            });
            if (isset(yii::$app->session['language'])) {
                yii::$app->language = yii::$app->session['language'];
            }
            if (yii::$app->getRequest()->getIsAjax()) {
                yii::$app->getResponse()->format = Response::FORMAT_JSON;
            } else {
                yii::$app->getResponse()->format = Response::FORMAT_HTML;
            }
        }
    }
}